package com.jxs.android.sxyw;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.EomsInfo;
import com.jxs.android.sxyw.data.OrderType;
import com.jxs.android.sxyw.data.UserInfo.Role;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class EmosDetail extends EomsAbstractDetail implements OnClickListener {

	private TextView work_order,// 工单号
			subject,// 主题
			end_time,// 处理时限
			resume_time,// 恢复时间
			area,// 所属地区
			specialty,// 专业
			business,// 业务
			company;// 公司

	private LinearLayout eoms_info_work_order_ll, eoms_info_subject_ll, eoms_info_company_ll;
	private EomsInfo eomsInfo;
	private int position = 0;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.eoms_detail);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		eoms_info_company_ll = (LinearLayout) findViewById(R.id.eoms_info_company_ll);
		eoms_info_subject_ll = (LinearLayout) findViewById(R.id.eoms_info_subject_ll);
		eoms_info_work_order_ll = (LinearLayout) findViewById(R.id.eoms_info_work_order_ll);
		eoms_info_subject_ll.setOnClickListener(this);
		eoms_info_work_order_ll.setOnClickListener(this);
		eoms_info_company_ll.setOnClickListener(this);

		work_order = (TextView) findViewById(R.id.eoms_info_work_order);
		subject = (TextView) findViewById(R.id.eoms_info_subject);
		end_time = (TextView) findViewById(R.id.eoms_info_end_time);
		resume_time = (TextView) findViewById(R.id.eoms_info_resume_time);

		area = (TextView) findViewById(R.id.eoms_info_area);
		specialty = (TextView) findViewById(R.id.eoms_info_specialty);
		business = (TextView) findViewById(R.id.eoms_info_business);
		company = (TextView) findViewById(R.id.eoms_info_company);
		initEomsInfo();
	}

	private void initEomsInfo() {
		if (null != getIntent() && null != getIntent().getExtras()) {
			eomsInfo = (EomsInfo) getIntent().getExtras().getSerializable("eomsInfo");
			position = getIntent().getExtras().getInt("position", 0);
		}

		if (null != eomsInfo) {
			work_order.setText(eomsInfo.getCase_no());
			subject.setText(eomsInfo.getTitle());
			end_time.setText(eomsInfo.getPlan_finish_time());
			resume_time.setText(StringUtils.isEmpety(eomsInfo.getResume_time()) ? "无" : eomsInfo.getResume_time());

			area.setText(eomsInfo.getCarrier_name());
			specialty.setText(eomsInfo.getSpecialty_name());
			business.setText(eomsInfo.getBusiness_name());
			company.setText(eomsInfo.getCompany_name());
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == eoms_info_subject_ll.getId()) {
			if (null != eomsInfo) {
				requestDesDetail("主题", eomsInfo.getTitle());
			}
		} else if (v.getId() == eoms_info_work_order_ll.getId()) {
			if (null != eomsInfo) {
				requestDesDetail("工单号", eomsInfo.getCase_no());
			}
		} else if (v.getId() == eoms_info_company_ll.getId()) {
			if (null != eomsInfo) {
				requestDesDetail("代维公司", eomsInfo.getCompany_name());
			}
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_detail);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("签收");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {
				if (null != SelfApplication.getUserInfo() && null != SelfApplication.getUserInfo().getRole()) {
					if (Role.DISPACHERR.ordinal() == SelfApplication.getUserInfo().getRole().ordinal()) {// 调度员
						signAlert(eomsInfo);
					} else if (Role.MAINTAINER.ordinal() == SelfApplication.getUserInfo().getRole().ordinal()) {// 代维人员
						process(eomsInfo);
					}
				}
			}
		});
	}

	@Override
	protected void resultOk() {
		PopMsg.show("处理成功！");
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putInt("position", position);
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		this.finish();
	}

	@Override
	protected void sendNext(EomsInfo eomsInfo) {
		Intent intent = new Intent(this, EomsNextPerson.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("eomsInfo", eomsInfo);
		intent.putExtras(bundle);
		startActivityForResult(intent, REQ_SEND_NEXT);
	}

	@Override
	protected void process(EomsInfo eomsInfo) {
		Intent intent = new Intent();
		if (eomsInfo.getOrderType().index == OrderType.EOMS_ALARM.index) {
			intent.setClass(this, EomsAlermProcess.class);
		} else if (eomsInfo.getOrderType().index == OrderType.EOMS_POWER.index) {
			intent.setClass(this, EomsPowerProcess.class);
		}
		Bundle bundle = new Bundle();
		bundle.putSerializable("eomsInfo", eomsInfo);
		intent.putExtras(bundle);
		startActivityForResult(intent, REQ_DIRECT_PROCESS);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			resultOk();
		}
	}
}
